package com.qfedu.service.impl;

import com.qfedu.dao.AccountDao;
import com.qfedu.dao.CardsDao;
import com.qfedu.dao.CreditCardDao;
import com.qfedu.dao.impl.AccountDaoImpl;
import com.qfedu.dao.impl.CardsDaoImpl;
import com.qfedu.dao.impl.CreditCardDaoImpl;
import com.qfedu.pojo.Account;
import com.qfedu.pojo.Cards;
import com.qfedu.pojo.CreditCard;
import com.qfedu.service.AccountService;
import com.qfedu.utils.DruidUtil;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;

public class AccountServiceImpl implements AccountService {

    AccountDao accountDao = new AccountDaoImpl();

    @Override
    public void openAccount(Account account) {
        accountDao.saveAccount(account);
    }

    @Override
    public boolean existsPhone(String phone) {
        Account account = accountDao.queryAccountByPhone(phone);
        if(account == null){
            return false;
        }
        return true;
    }

    @Override
    public boolean existsIdcode(String idcode) {
        Account account1 = accountDao.queryAccountByPhone(idcode);
        if(account1 == null){
            return false;
        }
        return true;
    }

    @Override
    public Account queryAccountByIdcode(String idcode) {
        return accountDao.queryAccountByIdcode(idcode);
    }

    @Override
    public Account forgetCheck(String name, String phone, String idcode) {
        return accountDao.forgetCheck(name, phone, idcode);
    }

    @Override
    public Account queryIdcodeByPhone(String phone) {
        return accountDao.queryIdcodeByPhone(phone);
    }

    /*------------------------------------------------------------------------------------------------*/
    CardsDao cardsDao = new CardsDaoImpl();
    CreditCardDao creditCardDao = new CreditCardDaoImpl();

    @Override
    public Account findAccountIdcode(String s) {
        return accountDao.findAccountIdcode(s);
    }

    @Override
    public void transfer(Account source, Cards target, int money) {
        Connection connection = DruidUtil.getConnection();
        try {
            connection.setAutoCommit(false);
            accountDao.outMoney(source, money, connection);
            //int i = 1/0;
            //accountDao.inMoney(target,money,connection);
            cardsDao.inMoney(target, money, connection);
            connection.commit();
        } catch (Exception throwables) {
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }
    }

    @Override
    public Account findMoney() {
        return accountDao.findMoney();
    }

    @Override
    public Account find1(Account account2) {
        return accountDao.find1(account2);
    }

    @Override
    public Account findAccountPhone(String s) {
        return accountDao.findAccountPhone(s);
    }

    @Override
    public void transefer2(CreditCard source, Account target, double money) {
        Connection connection = DruidUtil.getConnection();
        try {
            connection.setAutoCommit(false);
            creditCardDao.outMoney2(source, money, connection);
            //int i = 1/0;
            //accountDao.inMoney(target,money,connection);
            accountDao.inMoney2(target, money - money * 0.05, connection);
            connection.commit();
        } catch (SQLException throwables) {
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }
    }

    @Override
    public Account findPassword() {
        return accountDao.findPassword();
    }

    @Override
    public int updateMoney(Account account ,BigDecimal add) {
        return accountDao.updateMoney(account,add);
    }
}
